package cn.bluedot.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * 备份数据库
 * @author hxp
 * 2019年5月9日 上午8:58:38
 */
public class BackupAndRecoverDatabase {
    
    public static final Runtime rt = Runtime.getRuntime();
    
    /**
     * 备份整个数据库 思路是通过调用cmd 命令的方式
     * @param dataBaseName 数据库名
     */
    public static void backup(String dataBaseName) {        
        try {
             //调用mysql的安装目录命令  命令包含的信息有数据库 IP 用户名 密码 数据库名
            Process process = rt.exec("C:\\Program Files\\MySQL\\MySQL Server 5.7\\bin\\mysqldump -h localhost -uroot -proot "+dataBaseName);
             // 把进程执行中的控制台输出信息写入.sql文件，即生成了备份文件。注：如果不对控制台信息进行读出，则会导致进程堵塞无法运行
            //控制台的输出信息作为输入流
            InputStream in = process.getInputStream();
            // 设置输出流编码为utf-8。这里必须是utf-8，否则从流中读入的是乱码
            InputStreamReader reader = new InputStreamReader(in, "utf-8");
            
            String inStr;
            StringBuffer buffer = new StringBuffer();
            String outStr;
            //组合控制台输出的信息字符串
            BufferedReader  bufferReader = new BufferedReader(reader);
            while((inStr=bufferReader.readLine())!=null) {
                buffer.append(inStr+"\r\n");
            }
            outStr = buffer.toString();
            
            //用来做到入用的目标文件
            String backupFileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())+".sql";
            FileOutputStream  out = new FileOutputStream("d:\\"+backupFileName);
            OutputStreamWriter  writer = new OutputStreamWriter(out, "utf-8");
            writer.write(outStr);
            writer.flush();
            
            //关闭资源
            in.close();
            reader.close();
            bufferReader.close();
            writer.close();
            out.close();

            
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }


    /**
     * 恢复整个数据库 
     * @param dataBaseName  数据库名
     * @param fromBackupFilePath   目标恢复文件路径
     */
    public static void recover(String dataBaseName, String fromBackupFilePath) {
        try {
            Process process  = rt.exec("C:\\Program Files\\MySQL\\MySQL Server 5.7\\bin\\mysql.exe -hlocalhost -uroot -proot --default-character-set=utf8 "+dataBaseName);
            OutputStream out = process.getOutputStream();
            BufferedReader bufferReader = new BufferedReader(new InputStreamReader(new FileInputStream(fromBackupFilePath), "utf-8"));
            
            String str = null;
            StringBuffer  buffer =  new StringBuffer();
            while((str=bufferReader.readLine())!=null) {
                buffer.append(str+"\r\n");
            }
            str  = buffer.toString();
            
            OutputStreamWriter writer = new OutputStreamWriter(out, "utf-8");
            writer.write(str);
            writer.flush();
            
            out.close();
            bufferReader.close();
            writer.close();
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
   
}
